package game.scene.map.event;
import game.scene.map.Model;
import java.util.Stack;
import game.scene.SceneStack;

import org.json.me.JSONArray;

public class EventPage {
  public Event event;
  private Model _model;
  private int _commandIndex;
  public JSONArray commands;
  private JSONArray _triggers, _validConditions, _route, _originalRoute;
  private EventParameters _initParameters;
  public boolean active, pauseCommand;
  private Stack commandstack;
  private EventCommands _eventCommands;
  public EventPage(Event event, Model model, JSONArray triggers, JSONArray validConditions, JSONArray commands, JSONArray route, EventParameters initParameters){
    this.event = event;
    _model = model;
    _triggers = triggers;
    _validConditions = validConditions;
    this.commands = commands;
    _route = route;

    _originalRoute = route;
    _initParameters = initParameters;
    _commandIndex = 0;
    active = false;
    pauseCommand = false;
    commandstack = new Stack();
    _eventCommands = new EventCommands(event, event, commands, model);
    //moveRunner
  }

  
  public boolean isValid(){
    boolean b = true;   
    try{
      for(int i = 0; i < _validConditions.length(); i++){
        if(!event.isConditionValid(_validConditions.getJSONArray(i))){ b = false;}
      }
    }catch(Exception e){
      System.out.println("isValid Error");
    }

    return b;
  }
  
  public boolean isTriggered(){
    boolean b = true;
    event.auto = true;
    try{
      for(int i = 0; i < _triggers.length(); i++){
        JSONArray trigger = _triggers.getJSONArray(i);

        String type = trigger.getString(0);
        if(type.equals("checked")){
          b = event.character.isChecked(event.getCharacter(trigger.getint(1)));
          //System.out.println("CHECKED: " + b);
          break;
        }
        if(type.equals("touched")){
          b = event.character.isTouched(event.getCharacter(trigger.getint(1)));
          break;
        }
        if(type.equals("touch")){
          b = event.getCharacter(trigger.getint(1)).isTouched(event.character);
          break;
        }
        if(type.equals("parallel")){
          event.auto = false;
          b = true;
          break;
        }
        if(type.equals("auto")){
          b = true;
          break;
        }
        if(type.equals("never")){
          b = false;
          break;
        }
      }
    }catch(Exception e){
      System.out.println("isTrigger Error:" + e);
    }
    return b;
  }

  public void executeCommand(SceneStack sceneStack){
    
    if(commandstack.isEmpty()){
      if(_eventCommands.active){
        _eventCommands.update(sceneStack, commandstack);
      }
    }else{
      EventCommands t = (EventCommands)(commandstack.lastElement());
      if(t.active){
        t.update(sceneStack, commandstack);
      }else{
        commandstack.pop();
      }
    }
    
    if(commandstack.isEmpty() && ! _eventCommands.active){
      onCommandFinish();
    }
  }

  public void onValidate(){
    event.character.passTile = _initParameters.passTile;
    event.character.passCharacters = _initParameters.passCharacters;
    event.character.passEvent = _initParameters.passEvent;
    event.character.dirFix = _initParameters.dirFix;
    event.character.stayAnime = _initParameters.stayAnime;
    event.character.moveAnime = _initParameters.moveAnime;
    event.character.hitX = _initParameters.hitX;
    event.character.hitY = _initParameters.hitY;
    event.character.offsetX = _initParameters.offsetX;
    event.character.offsetY = _initParameters.offsetY;
    event.moveCount = 0;

    event.character.resetPosition();
    event.reset();
    event.parameters = _initParameters.clone();

    event.setInitRoute(_originalRoute.getVector());

    event.setCharacterChip(_model, event.parameters.chipID, event.parameters.frameNo, event.parameters.dir);

    event.character.setSpeedType(event.parameters.speedType);

    event.character.alpha = event.parameters.alpha;
    event.character.layer = event.parameters.layer;
  }

  public void onCommandStart(){
    active = true;
    if(event.auto){event.player().pause = true;}
    event.pauseMovement = false;
  }

  private void onCommandFinish(){
   active = false;
   if(event.auto){event.player().pause = false;}
   event.pauseMovement = false;
   _eventCommands.reset();
  }
}
